package com.weixin.servlet;

import java.io.FileOutputStream;
import java.io.IOException;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.apache.poi.xwpf.usermodel.XWPFDocument;

import com.jspsmart.upload.File;
import com.jspsmart.upload.Files;
import com.jspsmart.upload.Request;
import com.jspsmart.upload.SmartUpload;
import com.weixin.entity.HouseGoods;
import com.weixin.service.IHouseGoodsService;
import com.weixin.service.impl.HouseGoodsServiceImpl;

import cn.afterturn.easypoi.word.WordExportUtil;
import cn.afterturn.easypoi.word.entity.WordImageEntity;
import javafx.scene.input.DataFormat;


@WebServlet("/SingleGoodsServlet/*")
public class SingleGoodsServlet extends HttpServlet {
	 private static Logger logger = Logger.getLogger(SingleGoodsServlet.class); 
	IHouseGoodsService housegoodsService=new HouseGoodsServiceImpl();
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String path = request.getPathInfo().substring(1);
		if("preShowGoods".equals(path)) {
			PreShowGoods(request,response);
		}else if("upoadPicture".equals(path)) {
			UpoadPicture(request,response);
		}else if("upoadIntrodcue".equals(path)) {
			UpoadIntrodcue(request,response);
		}else if("outGoodsInfo".equals(path)) {
			OutGoodsInfo(request,response);
		}
	}
	private void OutGoodsInfo(HttpServletRequest request, HttpServletResponse response) {
		int h_goods_id = Integer.valueOf(request.getParameter("h_goods_id"));
		try {
			HouseGoods houseGoods = housegoodsService.getAllAboutById(h_goods_id);
			Map<String, Object> map = new HashMap<String, Object>();
	        map.put("h_goods_id", houseGoods.getH_goods_id());
	        map.put("goods_name", houseGoods.getGoods_name());
	        map.put("unit", houseGoods.getUnit());
	        map.put("sum", houseGoods.getSum());
	        map.put("volume", houseGoods.getVolume());
	        map.put("storehouse_name", houseGoods.getStorehouse_name());
	        map.put("time",new SimpleDateFormat("yyyy-MM-dd").format(new Date()));
	        if(houseGoods.getRemark()!=null) {        	
	        	map.put("shuoming",houseGoods.getRemark());
	        }else {
	        	map.put("shuoming","无内容");
	        }
	        WordImageEntity image = new WordImageEntity();
	        image.setHeight(400);
	        image.setWidth(500);
	        image.setUrl(houseGoods.getPicture());
	        image.setType(WordImageEntity.URL);
	        map.put("picture", image);
	        try {
	            XWPFDocument doc = WordExportUtil.exportWord07("/mould/Image.docx",map);
	            FileOutputStream fos = new FileOutputStream("D:/HouseGoodsInfo.docx");
	            doc.write(fos);
	            
	            fos.close();
	            logger.error("导出单个商品信息，h_goods_id="+h_goods_id);
	            response.getWriter().print(1);
	        } catch (Exception e) {
	            e.printStackTrace();
	            logger.error(e);
	        }
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	private void UpoadIntrodcue(HttpServletRequest request, HttpServletResponse response) {
		int h_goods_id=Integer.valueOf(request.getParameter("h_goods_id"));
		String introduce=request.getParameter("introduce");
		try {
			
			if(housegoodsService.addIntroduceForGoods(introduce, h_goods_id)) {
				 logger.error("更改商品介绍：h_goods_id："+h_goods_id+"introduce："+introduce);
				response.getWriter().print(1);
			}else {
				response.getWriter().print(0);
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	private void UpoadPicture(HttpServletRequest request, HttpServletResponse response) {
		//int h_goods_id=Integer.valueOf(request.getParameter("h_goods_id"));
		//创建对象
		SmartUpload smart=new SmartUpload();
		try {
			smart.initialize(getServletConfig(),request,response);
			smart.setCharset("utf-8");
			smart.upload();
			//smart.save("/goodsImageUpoad");
			int h_goods_id =Integer.valueOf(smart.getRequest().getParameter("h_goods_id"));
			Files files=smart.getFiles();
			File file = files.getFile(0);
			String ext=file.getFileExt();
			String fileName=UUID.randomUUID().toString()+"."+ext;
			file.saveAs("/goodsImageUpoad/"+fileName,SmartUpload.SAVE_VIRTUAL);	
			if(housegoodsService.addPictureForGoos("/goodsImageUpoad/"+fileName, h_goods_id)) {
				HouseGoods houseGoods = housegoodsService.getAllAboutById(h_goods_id);
				request.setAttribute("houseGoods",houseGoods);
				request.getRequestDispatcher("/page/user/goodsManage/singleGoodsInfo.jsp").forward(request, response);
				logger.error("更改图片成功，图片名："+fileName);
			}else {
				HouseGoods houseGoods = housegoodsService.getAllAboutById(h_goods_id);
				request.setAttribute("houseGoods",houseGoods);
				request.getRequestDispatcher("/page/user/goodsManage/singleGoodsInfo.jsp").forward(request, response);
				logger.error("更改图片失败，图片名："+fileName);
			}			
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			logger.error(e);
		}
		
	}
	private void PreShowGoods(HttpServletRequest request, HttpServletResponse response) {
		int h_goods_id = Integer.valueOf(request.getParameter("h_goods_id"));
		try {			
			HouseGoods houseGoods = housegoodsService.getAllAboutById(h_goods_id);
			request.setAttribute("houseGoods",houseGoods);
			request.getRequestDispatcher("/page/user/goodsManage/singleGoodsInfo.jsp").forward(request, response);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}


}
